<?php
/**
 * 《电话号码》实现类
 *
 * @author 孙晓晔
 * @version $Id: Common.class.php 513 2009-11-11 07:31:44Z sunxy $
 */

class Number {
	var $dollar = 6.85;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';
	}

	/**
	 * 网络电话
	 */
	function vbuzzer($s, $cdr = 'ns_sip') {
		if (preg_match('/^1/i', $s)) {
			return Number::country(substr($s, 0), $cdr);
		}

		if (preg_match('/^011/i', $s)) {
			return Number::country(substr($s, 3), $cdr);
		}

		return $s;
	}

	/**
	 * 国外电话
	 */
	function country($s, $cdr = 'ns_sip') {
		if (preg_match('/^86/i', $s)) {
			$ret = Number::china('0' . substr($s, 2), $cdr);
			$ret['rate'] = 0.020 * $this->dollar;
			
			return $ret;			
		}

		if (preg_match('/^1/i', $s)) {
			$a = '1';
		} else {
			$a = substr($s, 0, 2);
		}

		$key = "country_" . $s;
		debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}
		
		$db = newdb(Constant::tbl_contact);
		$fmt = "SELECT id, info, info_id, name FROM %s WHERE replace(tel, '-', '') LIKE '%s' OR replace(mobile, '-', '') LIKE '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, "%". $s . "%", "%". $s . "%");
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['t'] = 'contact';
			
			return $row;
		}

		$db = newdb(Constant::tbl_rate);
		$fmt = "select * from %s where zone like '%s' order by zone desc";
		$sql = sprintf($fmt, Constant::tbl_rate, $a . '%');
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (preg_match("/^" . $row['zone'] . "/i", $s)) {
				$row['rate'] = $row['rate'] * $this->dollar;

				if (is_object($mc)) {
					$mc->set($key, $row, 0, _strtotime("1 day"));
				}

				return $row;
			}
		}

		return false;
	}

	/**
	 * 国内电话
	 */
	function china($s, $cdr = 'ns_sip') {
		if (preg_match('/^(11808|96688)/i', $s)) {
			$ret = $this->country(substr($s, 7), $cdr);
			$ret['rate'] = 0.39;
			
			return $ret;
		}

		if (preg_match('/^1(3|5|8)\d{9}$/i', $s)) {			
			$ret = Number::mobile(substr($s, 0, 11));
			$ret['rate'] = 0.3;
			
			return $ret;
		}

		if (preg_match('/^0/i', $s)) {
			if (preg_match('/^0(10|2)/i', $s)) {
				$ret = Number::fixed($s, substr($s, 0, 3));
				$ret['rate'] = 0.3;
					
				return $ret;
			}
			
			if (preg_match('/^01(3|5|8)\d{9}$/i', $s)) {
				$ret = Number::mobile(substr($s, 1, 11));
				$ret['rate'] = 0.3;
					
				return $ret;
			}
			
			$ret = Number::fixed($s, substr($s, 0, 4));
			$ret['rate'] = 0.3;
			
			return $ret;
		}
		
		$zone = '0755';
		if ($cdr == 'qx_sip') {
			$zone = '0769';
		}
		
		$ret = Number::fixed($zone . $s, $zone);
		$ret['rate'] = 0.22;
		
		return $ret;
	}

	/**
	 * 手机号码
	 * 
	 * @param string
	 * @return array
	 */
	function mobile($s) {
		$key = "mobile_" . $s;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$ret = Number::_mobile($s);
		if ($ret && is_object($mc)) {
			$ret['b'] = Number::_black($s);
			
			$mc->set($key, $ret, 0, _strtotime("1 day"));

			return $ret;
		}

		$db = newdb('emp');
		$fmt = "INSERT INTO %s(mobile, createtime) VALUES('%s', now())";
		$sql = sprintf($fmt, Constant::tbl_mobile, substr($s, 0, 7));
		// debug($sql);
		$db->query($sql);

		return $ret;
	}
	
	function _black($s) {
		$row = Common::x(Constant::tbl_blacklist, 'number', $s);
		if ($row['status'] == '1') {
			return 'b'; 
		}
		
		return '';
	}

	/**
	 * 手机号码
	 * 
	 * @param string
	 * @return array
	 */
	function _mobile($s) {
		$db = newdb(Constant::tbl_emp);
		$fmt = "SELECT id, name FROM %s WHERE mobile = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp, $s);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['t'] = 'emp';
			
			return $row;
		}
		
		$fmt = "SELECT id, info, info_id, name FROM %s WHERE mobile = '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $s);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['t'] = 'contact';
			
			return $row;
		}

		$fmt = "SELECT province, city FROM %s WHERE mobile = '%s'";
		$sql = sprintf($fmt, Constant::tbl_mobile, substr($s, 0, 7));
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['name'] = $row['province'] . $row['city'];

			return $row;
		}

		return false;
	}

	/**
	 * 固定电话
	 * 
	 * @param string 
	 * @param string
	 * @return array
	 */
	function fixed($s, $zone) {
		$key = "fixed_" . $s;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$ret = Number::_fixed($s, $zone);
		if ($ret && is_object($mc)) {
			$ret['b'] = Number::_black($s);
			
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 * 固定电话
	 * 
	 * @param string 
	 * @param string
	 * @return array
	 */
	function _fixed($s, $zone) {
		$db = newdb(Constant::tbl_contact);
		$fmt = "SELECT id, info, info_id, name FROM %s WHERE replace(tel, '-', '') LIKE '%s'";
		$sql = sprintf($fmt, Constant::tbl_contact, $s . "%");
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['t'] = 'contact';
			
			return $row;
		}

		$db = newdb(Constant::tbl_zone);
		$fmt = "SELECT * FROM %s WHERE zone = '%s'";
		$sql = sprintf($fmt, Constant::tbl_zone, $zone);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$row['name'] = $row['province'] . $row['city'];

			return $row;
		}

		return false;
	}
}